<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入参数名"
        v-model="queryForm.name"
      >
      </el-input>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'dict:add'"
        class="filter-item"
        style="margin-left: 10px;"
        @click="openCreateDialog(null)"
        type="success"
        icon="el-icon-edit"
        >新增</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="name" label="名称" width="650" align="center">
      </el-table-column>
      <el-table-column prop="code" label="参数代码" width="250" align="center">
      </el-table-column>
      <el-table-column prop="value" label="参数值" width="250" align="center">
      </el-table-column>
      <el-table-column prop="seq" label="排序" width="250" align="center">
      </el-table-column>
      <el-table-column
        width="300"
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'dict:update'"
            @click="editDict(scope.row)"
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            title="编辑"
          ></el-button>
          <el-button
            v-permission="'dict:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`${editDictForm.rowId ? '编辑' : '新增'}字典`"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form
        :model="editDictForm"
        label-width="100px"
        :rules="editDictFormRules"
        ref="editDictForm"
      >
        <el-form-item label="参数名称:" prop="name">
          <el-input v-model="editDictForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="参数代码:" prop="code">
          <el-input v-model="editDictForm.code" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="参数值:" prop="value">
          <el-input v-model="editDictForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序:" prop="seq">
          <el-input v-model="editDictForm.seq" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditDict">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryDict, deleteDict, addOrEditDict } from '@/api/system'

export default {
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      editDictForm: {
        rowId: undefined,
        name: '',
        code: '',
        value: '',
        seq: ''
      },
      editDictFormRules: {
        code: [{ required: true, message: '请输入参数代码', trigger: 'blur' }],
        value: [{ required: true, message: '请输入参数值', trigger: 'change' }],
        seq: [{ required: true, message: '请输入参数排序', trigger: 'change' }]
      },
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        title: ''
      },
      disabled: false,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryDict(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDict(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    editDict({ rowId, name, code, value, seq }) {
      this.openCreateDialog(rowId)
      this.editDictForm = {
        rowId: rowId,
        name: name,
        code: code,
        value: value,
        seq: seq
      }
    },
    openCreateDialog(rowId) {
      this.editDictForm = {}
      this.dialogFormVisible = true
      this.$refs.editDictForm && this.$refs.editDictForm.resetFields()
      this.disabled = rowId != null
    },
    addOrEditDict() {
      this.$refs.editDictForm.validate(valid => {
        if (valid) {
          addOrEditDict(this.editDictForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
